#!/usr/bin/python3
# 如果你没有安装 Python 3 ，那么修改这一行为 #!/usr/bin/python
# Script name: uname.py
# Purpose: Illustrate Python OOP capabilities to write shell scripts more easily
# License: GPL v3 (http://www.gnu.org/licenses/gpl.html)
# Copyright (C) 2016 Gabriel Alejandro Cánepa
# ​Facebook / Skype / G+ / Twitter / Github: gacanepa
# Email: gacanepa (at) gmail (dot) com
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see .
# REVISION HISTORY
# DATE        VERSION AUTHOR         CHANGE DESCRIPTION
# ---------- ------- --------------
# 2016-05-28 1.0     Gabriel Cánepa    Initial version
### 导入 os 模块
import os
### 将 os.uname() 的输出赋值给 systemInfo 变量
### os.uname() 会返回五个字符串元组（sysname, nodename, release, version, machine）
### 参见文档：https://docs.python.org/3.2/library/os.html#module-os
systemInfo = os.uname()
### 这是一个固定的数组，用于描述脚本输出的字段标题
headers = ["Operating system","Hostname","Release","Version","Machine"]
### 初始化索引值，用于定义每一步迭代中
### systemInfo 和字段标题的索引
index = 0
### 字段标题变量的初始值
caption = ""
### 值变量的初始值
values = ""
### 分隔线变量的初始值
separators = ""
### 开始循环
for item in systemInfo:
    if len(item) < len(headers[index]):
     ### 一个包含横线的字符串，横线长度等于item[index] 或 headers[index]
     ### 要重复一个字符，用引号圈起来并用星号（*）乘以所需的重复次数 
     separators = separators + "-" * len(headers[index]) + " "
     caption = caption + headers[index] + " "
     values = values + systemInfo[index] + " " * (len(headers[index]) - len(item)) + " "
    else:
     separators = separators + "-" * len(item) + " "
     caption =  caption + headers[index] + " " * (len(item) - len(headers[index]) + 1)
     values = values + item + " "
    ### 索引加 1
    index = index + 1
### 终止循环
### 输出转换为大写的变量（字段标题）名
print(caption.upper())
### 输出分隔线
print(separators)
# 输出值（systemInfo 中的项目）
print(values)
### 步骤：
### 1) 保持该脚本为 uname.py （或任何你想要的名字）
### 并通过如下命令给其执行权限：
### chmod +x uname.py
### 2) 执行它；
### ./uname.py